using System;
using System.IO;
using System.Security;
using Microsoft.Win32;
using Microsoft.SharePoint.Utilities;

namespace PS.WebParts.Rollup.Utils
{
    public static class EnvironmentUtil
    {
        public static string GetWSSSiteDefFolder()
        {
            string value = GetWSSFolder();
            return Path.Combine(value, @"template\SiteTemplates");
        }

        public static string GetFieldsFolder()
        {
            return Path.Combine(GetWSSFolder(), @"TEMPLATE\FEATURES\fields\");
        }
        
        public static string GetFieldsFile()
        {
            return Path.Combine(GetFieldsFolder(), @"fieldswss.xml");
        }

        public static string GetWSSFolder()
        {
            string folder = SPUtility.GetGenericSetupPath(string.Empty);

            return folder;

            //MJM - Code below is version dependent

            string key = @"SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\12.0";
            string name = "Location";

            string wss12Dir;
            try
            {
                RegistryKey regKey = Registry.LocalMachine.OpenSubKey(key);
                wss12Dir = regKey.GetValue(name) as string;
                regKey.Close();
            }
            catch (SecurityException)
            {
                wss12Dir = String.Empty;
            }
            catch (ArgumentNullException)
            {
                wss12Dir = String.Empty;
            }
            catch (ArgumentException)
            {
                wss12Dir = String.Empty;
            }
            catch (ObjectDisposedException)
            {
                wss12Dir = String.Empty;
            }
            catch (IOException)
            {
                wss12Dir = String.Empty;
            }
            catch (UnauthorizedAccessException)
            {
                wss12Dir = String.Empty;
            }

            return wss12Dir;
        }
    }
}